import { BaseWindow } from "../../../../component/window/BaseWindow";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { ItemData } from "../../../resource/info/ItemData";
import { SRandPetSkillActive } from "../../../../../Message/Game/SRandPetSkillActive";
import { Language } from "../../../../../extend/language/Language";
import { ItemsUtil } from "../../shortcuts/item/ItemsUtil";
import { ObjCreate } from "../../common/ObjCreate";
import { GlobalStyle } from "../../../../common/global/GlobalStyle";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { SkillConfig } from "../../../resource/SkillConfig";
import { EPrictUnit } from "../../../../../Message/Public/EPrictUnit";
import { EBind } from "../../../../../Message/Public/EBind";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { PulseSharedObject } from "../../player/PulseSharedObject";
import { RefreshConfirmWindow } from "./RefreshConfirmWindow";
import { PetController } from "../../../control/PetController";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { CheckBoxWin } from "../../common/alertwins/CheckBoxWin";
import { Cache } from "../../../cache/Cache";
import { EActiveType } from "../../../../../Message/Public/EActiveType";
import { GameConst } from "../../../../component/gconst/GameConst";
import { TilePanel } from "../../equipment/TilePanel";
import { BatchRefreshRenderer } from "./BatchRefreshRenderer";
import { StarPanel } from "./StarPanel";
import { GComboBox } from "../../../../../com/mui/controls/GComboBox";
import { MailFriendItemCellRender } from "../../mail/MailFriendItemCellRender";
import { ResouceConst } from "../../../../component/gconst/ResouceConst";
import { ImagesConst } from "../../../resource/ImagesConst";
import { GTextFormat } from "../../../../common/GTextFormat";
import { PetMingMsgRollTips } from "../ming/PetMingMsgRollTips";
import { HTMLUtil } from "../../../../../com/gengine/utils/HTMLUtil";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { _itemData } from "../../../../../this/_itemData";
import { _toRefreshTimes } from "../../../../../this/_toRefreshTimes";
import { _cost } from "../../../../../this/_cost";
type int = number;
//class BatchRefreshWindow
    
    export  class BatchRefreshWindow extends BaseWindow
    {
        constructor()
        {
            super();
            this.init();
            this.registToolTip();
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onItemdataChangeHandler);
            return;
        }

        public set itemData(arg1: ItemData)
        {
            this._itemData = arg1;
            return;
        }

        public get skills(): Array<any>
        {
            return this._skills;
        }

        public set skills(arg1: Array<any>)
        {
            this._skills = arg1;
            this.updateDataProvider(arg1);
            return;
        }

        public get allTimes(): int
        {
            return this._allTimes;
        }

        public set allTimes(arg1: int)
        {
            this._allTimes = arg1;
            this._starPanel.num = arg1;
            return;
        }

        public showActiveInfo(arg1: Boolean, arg2: SRandPetSkillActive=null): void
        {
            var loc1=null;
            arg1 = arg1 && arg2 && arg2.nextRewardCost > 0;
            if (arg1) 
            {
                this._bottomSprite.y = 555;
                this.height = 590;
                if (this._activeSprite == null) 
                {
                    this._activeSprite = this.getActiveSprite();
                    this._activeSprite.x = 60;
                    this._activeSprite.y = 530;
                    this.addChild(this._activeSprite);
                }
                this._activeSprite.visible = true;
                if (arg2) 
                {
                    this._needCostTxt.htmlText = Language.getStringByParam(54579, arg2.nextRewardCost - arg2.totalCost);
                    loc1 = new ItemData(arg2.rewardItem);
                    this._getItemTxt.htmlText = Language.getStringByParam(54580, ItemsUtil.getItemName(loc1));
                }
            }
            else 
            {
                this._bottomSprite.y = 525;
                this.height = 560;
                if (this._activeSprite) 
                {
                    this._activeSprite.visible = false;
                }
            }
            return;
        }

        /* internal  */getActiveSprite(): Object /* flash.display.Sprite */
        {
            var loc1=new Object /* flash.display.Sprite */();
            ObjCreate.createTextField(Language.getString(54581), 160, 0, 70, 20, loc1, GlobalStyle.textFormat5);
            this._needCostTxt = ObjCreate.createTextField(Language.getStringByParam(54579, 0), 220, 0, 70, 20, loc1, GlobalStyle.textFormat5);
            this._getItemTxt = ObjCreate.createTextField(Language.getString(54580), 15, 0, 445, 20, loc1, GlobalStyle.textFormat8);
            this._getItemTxt.autoSize = flash.text.TextFieldAutoSize.RIGHT;
            return loc1;
        }

        /* internal  */updateDataProvider(arg1: Array<any>): void
        {
            var loc2=null;
            var loc1=new DataProvider();
            var loc3=0;
            while (loc3 < 12) 
            {
                loc2 = SkillConfig.instance.getInfoByName(arg1[loc3] as int);
                loc1.addItem({"itemData": _itemData, "skill": loc2});
                ++loc3;
            }
            this._listPanel.dataProvider = loc1;
            return;
        }

        /* internal  */onRefreshBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var e: Object /* flash.events.MouseEvent */;
            var unit: int;
            var onCostAlertHandler: Function;

            var loc1;
            unit = 0;
            e = arg1;
            if (this._itemData) 
            {
                unit = this._costCombo.selectedItem.id;
                if (unit == EPrictUnit._EPriceUnitGoldBind && this._itemData.itemInfo.item.bind == EBind._EBindNo) 
                {
                    onCostAlertHandler = (arg1: int): void
                    {
                        if (arg1 == Alert.OK) 
                        {
                            doRefresh(unit);
                        }
                        return;
                    }
                    Alert.show(Language.getString(54714), null, Alert.OK | Alert.CANCEL, null, onCostAlertHandler);
                }
                else 
                {
                    this.doRefresh(unit);
                }
            }
            return;
        }

        /* internal  */doRefresh(arg1: int): void
        {
            var unit: int;
            var isNeedAlert: Boolean;
            var clickHandler: Function;

            var loc1;
            unit = arg1;
            isNeedAlert = PulseSharedObject.isTodayNotTips(RefreshConfirmWindow.confirmKey);
            if (isNeedAlert) 
            {
                this.dealAlertHighSkill();
            }
            else 
            {
                clickHandler = (arg1: Boolean): void
                {
                    if (arg1) 
                    {
                        dealAlertHighSkill();
                    }
                    _confirmWindow.hide();
                    return;
                }
                if (this._confirmWindow == null) 
                {
                    this._confirmWindow = new RefreshConfirmWindow();
                }
                this._confirmWindow.updateData(unit, this._cost, this._toRefreshTimes, clickHandler);
                this._confirmWindow.show();
            }
            return;
        }

        /* internal  */dealAlertHighSkill(): void
        {
            var unit: int;
            var onGoOnClickHandler: Function;

            var loc1;
            unit = 0;
            onGoOnClickHandler = null;
            onGoOnClickHandler = (arg1: int, arg2: Boolean): void
            {
                if (arg1 == Alert.OK) 
                {
                    PetController.isBatchRefresh = true;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetRandSkillProp, {"uid": _itemData.uid, "unit": unit, "times": _toRefreshTimes}));
                    Alert.resetToDefault();
                }
                _isNeedAlert = !arg2;
                return;
            }
            unit = this._costCombo.selectedItem.id;
            if (this._isNeedAlert && this.isContainHighSkill()) 
            {
                Alert.alertWinRenderer = CheckBoxWin;
                Alert.checkBoxLabel = Language.getString(54125);
                Alert.show(Language.getString(54126), null, Alert.OK | Alert.CANCEL, null, onGoOnClickHandler);
            }
            else 
            {
                PetController.isBatchRefresh = true;
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetRandSkillProp, {"uid": this._itemData.uid, "unit": unit, "times": _toRefreshTimes}));
            }
            return;
        }

        /* internal  */isContainHighSkill(): Boolean
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < this._skills.length) 
            {
                loc1 = SkillConfig.instance.getInfoByName(this._skills[loc2] as int);
                if (loc1.skillLevel == 4) 
                {
                    return true;
                }
                ++loc2;
            }
            return false;
        }

        public clear(): void
        {
            this._itemData = null;
            this.skills = [];
            this.allTimes = 0;
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show(arg1, arg2);
            var loc1=Cache.instance.pet.petRandSkillActive;
            if (loc1) 
            {
                this.showActiveInfo(true, loc1);
            }
            else 
            {
                this.showActiveInfo(false, null);
            }
            return;
        }

        public /* override */ hide(): void
        {
            super.hide();
            this._isNeedAlert = true;
            return;
        }

        /* internal  */onItemdataChangeHandler(arg1: Object=null): void
        {
            if (this.isHide == false) 
            {
                if (this._itemData) 
                {
                    this._itemData = Cache.instance.pack.backPackCache.getPropItemDataByUid(this._itemData.uid);
                }
            }
            return;
        }

        public showMsg(arg1: string): void
        {
            this._msgRollTips.showMsg(arg1, 1);
            return;
        }

        /* internal  */init(): void
        {
            this._costDp = new DataProvider();
            if (Cache.instance.active.isActiveInEffect(EActiveType._EActiveTypeRandPetSkillCost)) 
            {
                this._cost = GameConst.PetRandSkillCostActive;
            }
            else 
            {
                this._cost = GameConst.PetRandSkillCost;
            }
            this._costDp.addItem({"id": 2, "label": _cost + Language.getString(69905)});
            this._costDp.addItem({"id": 3, "label": _cost + Language.getString(69907)});
            this.setSize(535, 560);
            this.titleHeight = 28;
            this.title = Language.getString(54120);
            this._listPanel = new TilePanel();
            this._listPanel.verticalGap = 3;
            this._listPanel.move(24, 45);
            this._listPanel.setSize(520, 440);
            this._listPanel.columnWidth = 120;
            this._listPanel.rowHeight = 140;
            this._listPanel.setStyle("cellRenderer", BatchRefreshRenderer);
            this.addChild(this._listPanel);
            this._starPanel = new StarPanel();
            this._starPanel.x = 145;
            this._starPanel.y = 490;
            this.addChild(this._starPanel);
            this._bottomSprite = new Object /* flash.display.Sprite */();
            this._bottomSprite.y = 525;
            this.addChild(this._bottomSprite);
            ObjCreate.createTextField(Language.getString(54121), 35, 5, 250, 20, this._bottomSprite);
            this._refreshBtn = ObjCreate.createGButton(Language.getString(54122), 230, 0, 70, 24, this._bottomSprite);
            this._refreshBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onRefreshBtnClickHandler);
            ObjCreate.createTextField(Language.getString(54123), 310, 5, 100, 20, this._bottomSprite);
            this._costCombo = new GComboBox();
            this._costCombo.move(390, 0);
            this._costCombo.width = 100;
            this._costCombo.height = 20;
            this._costCombo.buttonMode = true;
            this._costCombo.styleName = "GComboboxStyle";
            this._costCombo.drawNow();
            this._costCombo.dropdown.setStyle("cellRenderer", MailFriendItemCellRender);
            this._costCombo.dropdown.setStyle("skin", ResouceConst.getScaleBitmap(ImagesConst.WindowCenterB));
            this._costCombo.textField.setStyle("textFormat", new GTextFormat("", 12, 11661308, false, false, false, "", "", flash.text.TextFormatAlign.CENTER));
            this._costCombo.dataProvider = this._costDp;
            this._costCombo.selectedIndex = 0;
            this._bottomSprite.addChild(this._costCombo);
            this._msgRollTips = new PetMingMsgRollTips();
            this._msgRollTips.x = 230;
            this._msgRollTips.y = 520;
            this.addChild(this._msgRollTips);
            return;
        }

        /* internal  */registToolTip(): void
        {
            this._refreshBtn.toolTipData = HTMLUtil.addColor("<textFormat leading=\'5\'>" + Language.getString(54124) + "</textFormat>", "#ffffff");
            return;
        }

        /* internal  */onBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.target as GButton;
            if (loc1) 
            {
                if (loc1.name != "ok") 
                {
                    this.hide();
                }
                else 
                {
                    this.hide();
                }
            }
            return;
        }

        public get itemData(): ItemData
        {
            return this._itemData;
        }

        /* internal */ /* var */_listPanel: TilePanel;

        /* internal */ /* var */_starPanel: StarPanel;

        /* internal */ /* var */_refreshBtn: GButton;

        /* internal */ /* var */_costCombo: GComboBox;

        /* internal */ /* var */_confirmWindow: RefreshConfirmWindow;

        /* internal */ /* var */_activeSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_needCostTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_getItemTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_bottomSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_costDp: DataProvider;

        /* internal */ /* var */_itemData: ItemData;

        /* internal */ /* var */_skills: Array<any>;

        /* internal */ /* var */_allTimes: int;

        /* internal */ /* var */_toRefreshTimes: int=12;

        /* internal */ /* var */_isNeedAlert: Boolean=true;

        /* internal */ /* var */_cost: int;

        /* internal */ /* var */_msgRollTips: PetMingMsgRollTips;
    }
